-+<template>
    <div class="login">
        <!-- 文字 -->
        <div class="left">
            <h2>ERP管理系统</h2>
        </div>
        <!-- 登录列表 -->
        <div class="right">
            <h3>用户登录</h3>
            <el-form :model="loginForm" :rules="rules" ref="loginForm" label-width="100px">
                <el-form-item prop="username">
                    <el-input v-model="loginForm.username" placeholder="账号"></el-input>
                </el-form-item>
                <el-form-item prop="password">
                    <el-input v-model="loginForm.password" placeholder="密码"></el-input>
                </el-form-item>
                <el-form-item prop="captcha">
                   
                        <el-col :span="14"><div class="grid-content bg-purple">
                            <el-input v-model="loginForm.captcha" placeholder="验证码"></el-input>
                        </div></el-col>
                        <el-col :span="8"><div class="grid-content bg-purple" @click="exImg()"><img :src="imgPath" alt=""></div></el-col>
                
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="submitForm('loginForm')">立即登录</el-button>
                </el-form-item>
            </el-form>

        </div>
    </div>
</template>

<script>
// import { uuid } from 'vue-uuid';
export default {
    data() {
        return {
            imgPath:'',
            loginForm: {
                username: 'admin',
                password: '123456',
                captcha:'',
                remember_me:'flase',
                checkKey:''
            },
            rules: {
                username: [
                    { required: true, message: '请输入用户名', trigger: 'blur' },
                    { min: 5, max: 15, message: '长度在 5 到 15 个字符', trigger: 'blur' }
                ],
                password: [
                    { required: true, message: '请输入密码', trigger: 'blur' },
                ],
                captcha:[ { required: true, message: '请输入验证码', trigger: 'blur' },]
            },
        }
    },
    methods: {
        exImg(){
           this.getcaptcha() 
        },
        //获取验证码
        async getcaptcha(){
            this.loginForm.checkKey = new Date().getTime();//时间戳
            let T =  (new Date().getTime())/1000;  
            const { data:res } =  await this.axios.get(`http://8.142.74.15:48082/jeecg-boot/sys/randomImage/${this.loginForm.checkKey}`,{
                params:{_t: T},
                responseType:"json"
            });
            // console.log(res);
            // let blob = new Blob([res],{type:"image/jpeg"});
            // this.imgPath = window.URL.createObjectURL(blob);
            this.imgPath = res.result;
        },


        submitForm(formName) {
            this.$refs[formName].validate(async (valid) => {
                if (valid) {
                    if (!valid) return this.$message.error("请填写正确表单信息");
                    const { data: res } = await this.axios.post("http://8.142.74.15:48082/jeecg-boot/sys/login",this.loginForm);
                    // const { data: res } = await this.axios.post("login", this.loginForm);
                    console.log(res);
                    this.$message.success("登陆成功");
                    sessionStorage.token = res.result.token;
                    sessionStorage.username = this.loginForm.username;
                    sessionStorage.password= this.loginForm.password;
                    this.$router.push("/home");
                    // this.$router.push({path:"/home",query:{username:this.loginForm.username}})
                } else {
                    console.log('error submit!!');
                    return false;
                }
            });
        },
    },
    mounted(){
        this.getcaptcha();
        // console.log(uuid.v1(),uuid.v1());
    },
}
</script>

<style lang="scss" scoped>
.login {
    min-height: 100%;
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-color: rgba(38, 50, 56, 0.6);
    overflow: hidden;

    .left {
        color: #fff;
        font-size: 34px;
        padding: 280px 978px 440px 250px;

        h2 {
            font-weight: 400;
        }
    }

    .right {
        position: absolute;
        top: 0;
        right: 0;
        padding: 150px 60px 180px;
        width: 470px;
        min-height: 100%;
        background-color: #fff;
        img{
            width: 134px;
            height: 34px;
            margin: 0 15px;
        }
        h3 {
            margin: 16px 0;
            font-size: 16px;
            text-align: left;
        }

        .el-input {
            margin-bottom: 22px;
        }

        .el-button {
            width: 100%;
            margin-top: 38px;
            color: #fff;
            background-color: #17b3a3;
            border-color: #17b3a3;
        }

        .el-button:hover {
            background: #45c2b5;
            border-color: #45c2b5;
            color: #fff;
        }
    }
}

.login::before {
    position: fixed;
    top: 0;
    left: 0;
    z-index: -1;
    width: 100%;
    height: 100%;
    content: "";
    background-image: url('../assets/images/loginbg.jpeg');
    background-size: cover;
}

::v-deep .el-form-item__content {
    margin-left: 0 !important;
    height: 45px !important;
}
</style>